package com.jiutong.meipengdai.ui.activitys;

import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.entity.BaseRespond;
import com.jiutong.meipengdai.entity.IdentifyCodeRespond;
import com.jiutong.meipengdai.http.model.interfaces.RegisterAndLoginBiz;
import com.jiutong.meipengdai.http.present.UserInfoPresent;
import com.jiutong.meipengdai.utils.StringUtil;
import com.jiutong.meipengdai.utils.TimerUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 忘记密码
 * Created by suxi on 2017/5/27.
 */

public class PwdFrogetActivity extends BaseActivity implements RegisterAndLoginBiz {
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.et_phone)
    EditText etPhone;
    @Bind(R.id.et_vercode)
    EditText etVercode;
    @Bind(R.id.tv_getcode)
    TextView tvGetcode;
    @Bind(R.id.et_newpwd)
    EditText etNewpwd;
    @Bind(R.id.btn_submit)
    Button btnSubmit;
    private UserInfoPresent userInfoPresent;

    private String identifyCode;

    private TimerUtil timerUtil;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_password_reset;
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        tvTitle.setText("找回密码");
        etPhone.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        //初始化倒计时
        timerUtil = new TimerUtil(tvGetcode);
        timerUtil.setCountTimer();

        userInfoPresent = new UserInfoPresent(this, this);
    }

    @Override
    public String getOriPwd() {
        return null;
    }

    @Override
    public String getNewPwd() {
        return etNewpwd.getText().toString().trim();
    }

    @Override
    public String getRelName() {
        return null;
    }

    @Override
    public String getIdCard() {
        return null;
    }

    @Override
    public String getEmail() {
        return null;
    }

    @Override
    public String getNewPhone() {
        return null;
    }

    @Override
    public String getCardNumber() {
        return null;
    }

    @Override
    public void onVerCodeGetSuccess(IdentifyCodeRespond subjects) {
        IdentifyCodeRespond respond = (IdentifyCodeRespond) subjects;
        if (respond.getResCode().equals("200")) {
            timerUtil.startTimer();
            identifyCode = respond.getData();
        } else {
            toastShortShow(respond.getResDesc());
        }
    }

    @Override
    public void onSuccess(Object object) {
        BaseRespond respond = (BaseRespond) object;
        if (respond.getResCode().equals("200")) {
            toastShortShow("修改密码成功");
            openActivity(LoginActivity.class);
            finish();
        } else {
            toastShortShow(respond.getResDesc());
        }
    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public String getPhoneNum() {
        return etPhone.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return etNewpwd.getText().toString().trim();
    }

    @Override
    public String getVerificatCode() {
        return etVercode.getText().toString().trim();
    }

    @OnClick({R.id.iv_back, R.id.et_phone, R.id.tv_getcode, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.et_phone:
                break;
            case R.id.tv_getcode:
                if (!isRightPhone())
                    return;
                userInfoPresent.getAuthCode();
                break;
            case R.id.btn_submit:
                if (isRightAction())
                    userInfoPresent.forgetPwd();
                break;
        }
    }

    /**
     * 验证手机号是否正确
     */
    private boolean isRightPhone() {
        if (TextUtils.isEmpty(getPhoneNum())) {
            toastShortShow("请输入手机号码");
            return false;
        }

        if (!StringUtil.isMobileNumber(getPhoneNum())) {
            toastShortShow("手机号码格式有误，请输入正确手机号码");
            return false;
        }
        return true;
    }

    /**
     * 操作是否正确
     *
     * @return
     */
    private boolean isRightAction() {
        if (!isRightPhone()) {
            return false;
        }

        if (TextUtils.isEmpty(getVerificatCode())) {
            toastShortShow("请输入手机验证码");
            return false;
        }

        if (TextUtils.isEmpty(getPassword())) {
            toastShortShow("请输入密码");
            return false;
        }

        if (!identifyCode.equals(getVerificatCode())) {
            toastShortShow("您输入的验证码不正确，请重新输入");
            return false;
        }
        if (getPassword().length() < 8 || getPassword().length() > 16) {
            toastShortShow("请输入8-16位数的密码");
            return false;
        }
        return true;
    }
}
